<template>
	<view class="p-2">
		<u-navbar title="收银台" placeholder autoBack></u-navbar>
		<view class="flex align-center justify-center flex-column" style="height: 360rpx;">
			<view class="flex align-center">
				<view class="font-30 text-light6">支付剩余时间</view>
				<u-count-down :time="timestamp" format="mm:ss" autoStart @change="onChange">
					<view class="flex align-center">
						<view class="font-30 text-light6">{{ timeData.minutes }}</view>
						<view class="font-30 text-light6">:</view>
						<view class="font-30 text-light6">{{ timeData.seconds }}</view>
					</view>
					
				</u-count-down>
			</view>
			<view class="text-black font-weight-bold mt-3" style="font-size: 56rpx;">￥{{parmar.totalPrice}}</view>
		</view>

		<view class="bg-white" style="border-radius: 20rpx;">
			<view class="flex align-center justify-between bg-white px-2 border-bottom" style="height: 140rpx;"
				v-for="(item,index) in palments" :key="index" @click="changepay(item,index)">
				<u-image :src="item.icon" width="64rpx" height="64rpx"></u-image>
				<view class="ml-2 font-28">{{item.name}}</view>
				<view class="ml-auto flex align-center">
					<view v-if="index===0" class="mr-2 font-24 text-light6">余额:￥{{money}}</view>
					<u-image :src="curIndex===index?'/static/select.png':'/static/noselect.png'" width="30"
						height="30">
					</u-image>
				</view>
			</view>
		</view>


		<commonButton :title="`确认支付 ￥${parmar.totalPrice}`" @click="submint"></commonButton>




	</view>
</template>

<script>
	export default {
		data() {
			return {
				curIndex: 0,
				curName:'',
				money: '',
				timestamp: 10 * 60 * 1000,
				timeData: {},
				parmar:{},
				pay_way:3
			}
		},
		computed: {
			palments() {
				if (uni.$u.platform === 'mp') {
					return [{
						icon: '/static/pay/qianpay.png',
						name: '钱包余额'
					}, {
						icon: '/static/pay/wxpay.png',
						name: '微信'
					}]

				} else {
					return [ {
						icon: '/static/pay/wxpay.png',
						name: '微信'
					}]
				}
			}
		},
		onLoad(e) {
			this.parmar = e
			this.getData()
		},
		methods: {
			getData() {
				this.$api.user().then(res => {
					if (res.code === 1) {
						this.money = res.data.money
					}
				})
			},
			changepay(item, index) {
				this.curIndex = index
				this.pay_way = item.name === '微信'?1:item.name==='支付宝'?2:3
			},
			onChange(e) {
				this.timeData = e
			},
			submint() {
				this.$api.submitorder(this.parmar).then(res=>{
					console.log(res)
					if(res.code === 1){
						// this.orderPay(res.data.order_id)
					}
				})
			},
			orderPay(orderId){
				let parmar = {
					order_id:orderId,
					pay_way: this.pay_way
				}
				if(uni.$u.platform === 'mp'){
					this.$api.wxpay(parmar).then(res=>{
						if(res.code === 1){
							// 余额支付
							if(this.pay_way===3){
								setTimeout(()=>{
									uni.$u.route({
										type:'redirectTo',
										url:'pages/user/order'
									})
								},500)
							}else{
								uni.requestPayment({
									provider:'wxpay',
									orderInfo:res.data,
									complete: (result) => {
										setTimeout(()=>{
											uni.$u.route({
												type:'redirectTo',
												url:'pages/user/order'
											})
										},500)
									}
								})
							}
							
							
							
						}
					})
				}else{
					
					this.$api.orderpay(parmar).then(res=>{
						if(res.code === 1){
							if(this.pay_way===3){
								setTimeout(()=>{
									uni.$u.route({
										type:'redirectTo',
										url:'pages/user/order'
									})
								},500)
							}else{
								uni.requestPayment({
									provider:this.pay_way===1?'wxpay':'alipay',
									orderInfo:res.data,
									complete: (result) => {
										setTimeout(()=>{
											uni.$u.route({
												type:'redirectTo',
												url:'pages/user/order'
											})
										},500)
									},
									success: (result) => {
										setTimeout(()=>{
											uni.$u.route({
												type:'redirectTo',
												url:'pages/user/order'
											})
										},500)
										
									}
								})
							}
							
							
							
						}
					})
					
					
					
				}
				
				
				
				
				
				
			}
		}
	}
</script>

<style lang="scss">
	
</style>